package com.coco.seck.service.impl;

import com.coco.common.constant.SeckGoodsEnum;
import com.coco.seck.mapper.SeckillGoodsMapper;
import com.coco.seck.pojo.TbSeckillGoods;
import com.coco.seck.service.SeckGoodsService;
import com.coco.seck.vo.SeckGoodsSearchVo;
import com.coco.seck.vo.SeckGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/7/18 15:38
 */
@Service
public class SeckGoodsServiceImpl implements SeckGoodsService {

    @Autowired
    private SeckillGoodsMapper goodsMapper;

    /**
     * 查询秒杀商品表
     * @param searchVo
     * @return
     */
    @Override
    public List<SeckGoodsVo> findSeckGoods(SeckGoodsSearchVo searchVo) {
        List<SeckGoodsVo> seckGoodsVoList = goodsMapper.findSeckGoods(searchVo);
        return seckGoodsVoList;
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, String status) {
        goodsMapper.updateStatus(id,status);
    }

    /**
     * 删除秒杀商品
     * @param id
     */
    @Override
    public void deleteSeckGoods(Long id) {
        goodsMapper.deleteSecGoods(id);
    }

    /**
     * 修改秒杀商品信息
     * @param id
     * @param seckillGoods
     */
    @Override
    public void updateSeckGoods(Long id, TbSeckillGoods seckillGoods) {
        seckillGoods.setId(id);
        goodsMapper.updateSeckGoods(seckillGoods);
    }

    /**
     * 添加秒杀商品
     * @param goodsList
     * @param id
     */
    @Override
    public void addSeckGoods(List<TbSeckillGoods> goodsList, Long id) {
        goodsList.forEach(item -> {
            item.setDeployId(id);
            Date now = new Date();
            //创建时间
            item.setCreateTime(now);
            //判断添加的秒杀商品的开启时间的状态
            if (now.after(item.getStartTime()) && now.before(item.getEndTime())){
                item.setSecStatus(SeckGoodsEnum.OPEN_SEC.getCode());
            }else {
                item.setSecStatus(SeckGoodsEnum.CLOSE_SEC.getCode());
            }
            //启用状态 默认未启用
            item.setStatus(SeckGoodsEnum.START_USING.getCode());
            item.setStockCount(item.getNum());
            //默认不删除
            item.setIsDelete(SeckGoodsEnum.UN_DELETE.getCode());
            //参与次数
            item.setNumberOfActive(0);
            goodsMapper.addSeckGoods(item);
        });
    }

    /**
     * 查询单条
     * @param id
     * @return
     */
    @Override
    public TbSeckillGoods findById(Long id) {
        return goodsMapper.findById(id);
    }
}
